package com.sunricher.telinkblemesh.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sunricher.telinkblemesh.R;
import com.sunricher.telinkblemeshlib.models.UartDaliManager;

import java.util.HashMap;
import java.util.List;

public class DaliGroupsActivity extends AppCompatActivity {

    private int gatewayAddress = 0;

    private EditText groupEditText;
    private Button onButton;
    private Button offButton;
    private EditText deviceEditText;
    private Button getGroupsButton;
    private Button addToGroupButton;
    private Button removeFromGroupButton;
    private TextView groupsResultTextView;
    private TextView commandResultTextView;

    private int getFullGroupId() {
        return getGroupId() | 0x80;
    }

    private int getGroupId() {
        int value = 0;
        String newValue = groupEditText.getText().toString();
        if (newValue != null && newValue.length() > 0) {
            value = Integer.parseInt(newValue);
        }
        return value;
    }

    public int getDeviceId() {
        int value = 0;
        String newValue = deviceEditText.getText().toString();
        if (newValue != null && newValue.length() > 0) {
            value = Integer.parseInt(newValue);
        }
        return value;
    }

    private UartDaliManager.Callback callback = new UartDaliManager.Callback() {
        @Override
        public void didExecuteCommandOK(int daliAddress, int gatewayAddress, int cmdType, int cmd) {
            String text = "OK, " + "daliAddress " + daliAddress + ", cmdType " + cmdType + ", cmd " + cmd;
            commandResultTextView.setText(text);
        }

        @Override
        public void didExecuteCommandFailed(int daliAddress, int gatewayAddress, int reason, int cmdType, int cmd) {
            String text = "Failed, " + "daliAddress " + daliAddress + ", cmdType " + cmdType + ", cmd " + cmd;
            commandResultTextView.setText(text);
        }

        @Override
        public void didGetDeviceGroups(List<Integer> groups, int daliAddress, int gatewayAddress) {
            String text = "Groups ";
            for (int i = 0; i < groups.size(); i++) {
                text += groups.get(i) + " ";
            }
            groupsResultTextView.setText(text);
        }

        @Override
        public void didAddDeviceToGroup(int daliAddress, int gatewayAddress) {
            commandResultTextView.setText("did add device to group");
        }

        @Override
        public void didRemoveDeviceFromGroup(int daliAddress, int gatewayAddress) {
            commandResultTextView.setText("did remove device from group");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dali_groups);

        gatewayAddress = getIntent().getIntExtra("gatewayAddress", 0);

        groupEditText = findViewById(R.id.group_et);
        onButton = findViewById(R.id.on_btn);
        offButton = findViewById(R.id.off_btn);
        deviceEditText = findViewById(R.id.device_et);
        getGroupsButton = findViewById(R.id.get_groups_btn);
        addToGroupButton = findViewById(R.id.add_to_group_btn);
        removeFromGroupButton = findViewById(R.id.remove_from_group_btn);
        groupsResultTextView = findViewById(R.id.groups_result_tv);
        commandResultTextView = findViewById(R.id.command_result_tv);

        onButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> value = new HashMap<>();
                value.put("ON_OFF", true);
                UartDaliManager.getInstance().updateDataPoints(gatewayAddress, getFullGroupId(), value);
            }
        });

        offButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> value = new HashMap<>();
                value.put("ON_OFF", false);
                UartDaliManager.getInstance().updateDataPoints(gatewayAddress, getFullGroupId(), value);
            }
        });

        getGroupsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UartDaliManager.getInstance().getDeviceGroups(gatewayAddress, getDeviceId(), getGroupId());
            }
        });

        addToGroupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UartDaliManager.getInstance().addDeviceToGroup(gatewayAddress, getDeviceId(), getGroupId());
            }
        });

        removeFromGroupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UartDaliManager.getInstance().removeDeviceFromGroup(gatewayAddress, getDeviceId(), getGroupId());
            }
        });

        UartDaliManager.getInstance().setCallback(callback);
    }

    @Override
    protected void onStop() {
        super.onStop();
        UartDaliManager.getInstance().setCallback(null);
    }
}